;;;;
;;; 69 positions startup + intro
;;
;   DASM 2.20

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

debug = 0; // 0 for release build
skiptimer = 0 ; 0 for release buid , 1 for no wait in timer procedure

*= $0801

loader = $8000
;;;;;;;;;;;;;;;;;;;;;
; dload stuff

iloader = $9000

loadfile = iloader
loadts = iloader+3
shutup = iloader+6
wakeup = iloader+9
ledoff = iloader+12
speederoff = iloader+15

;;;;;;;;;;;;;;;;;;;;;;;

_start






	jsr zakryj_start

	jsr $ff81 ; init screen

	lda #<text1
	ldy #>text1
	jsr $ab1e

	ldx $d030
	inx
	beq c64_det

	lda #<c128d
	ldy #>c128d
	jsr $ab1e
	jmp c128de
c64_det

	lda #<c64de
	ldy #>c64de
	jsr $ab1e

c128de

        jsr loader
	bcc ok1_

	lda #<text2
	ldy #>text2
	jsr $ab1e

	jmp *

ok1_
	cpx #1
	bne f1570

	lda #<t1541
	ldy #>t1541
	jsr $ab1e
	jmp ok2

f1570 	cpx #2
	bne f1571

	lda #<t1570
	ldy #>t1570
	jsr $ab1e
	jmp ok2

f1571 	cpx #3
	bne f1581

	lda #<t1571
	ldy #>t1571
	jsr $ab1e
	jmp ok2

f1581 	cpx #4
	bne ok2

	lda #<t1581
	ldy #>t1581
	jsr $ab1e

ok2
;;;;;;;;

	lda #<loado
	ldy #>loado
	jsr $ab1e

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        sei
;;;;;
;;;;  detetcting sid type (mathematica)
;;;
	jsr reset_sid

	LDA #$02
	STA $D40F
	LDA #$30
	STA $D412
	LDY #$00
	LDX #$00
sl3	LDA $D41B
	BMI sl2
	DEX
	BNE sl3
	DEY
	BNE sl3
	BEQ sl4
sl2	LDX #$01 	; 1 = 8580
sl4			; 0 = 6581
	bne d8580

	lda #<t6581
	ldy #>t6581
	jsr $ab1e

	jmp _startupend

d8580 	lda #<t8580
	ldy #>t8580
	jsr $ab1e
;;;;;;;;;;;;;;;;;;;;;;

_startupend

	lda #<inite
	ldy #>inite
	jsr $ab1e

	jmp zuoo

xxxx .byte $00
yyy .byte $00

zuoo	ldx #3
        ldy #255

	stx xxxx
	sty yyy

spacjaa

__c     lda $d012
        cmp #$f0
        bne __c
        ;dec $d020

        dec yyy
        bne xyza
        dec xxxx
        bne xyzc

 	jmp xyzb

xyzc    lda #223
	cmp $dc01
	bne xyza

; press CBM to skip entire intro and load main part
	lda #3
	sta $d020
;	sei
        lda #$06
        sta $01

	jmp loadmain

xyza	lda $dc01
	cmp #239
	bne spacjaa
;;;
xyzb
	;sei
	jsr zakryj_start2

	lda $d011
	eor #$10
	sta $d011

	ldx #3
	jsr timer

	;

	lda #$00
	sta $d020
	sta $d021

	tax
	tay

	jsr $1000

; setting IRQ

	sei
	lda #$f1
	sta $d01a
	lda #$7f
	sta $dc0d
	lda #$00
	sta $dc0e

        lda #$06
        sta $01

	lda #<irq
	ldy #>irq
	sta $0314
	sty $0315

	lda #$30
	sta $d012
	lda $d011
	and #127
	sta $d011

	cli

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SEQUENCE

	ldx #50
	jsr timer

.include showe000.asm
	lda $d011
	eor #$10
	sta $d011

.include blinkappr.asm

	ldx #100
	jsr timer

.include e000dapr.asm

	lda #0
	sta $d011

	ldx #20
	jsr timer

;



	sei

	lda #<irqentry
	ldy #>irqentry
	sta $fffe
	sty $ffff

	lda #48+5
	sta $01
	cli


	lda #$10

	ldx #0

fa1	sta $c000,x
	sta $c100,x
	sta $c200,x
	sta $c300,x
	dex
	bne fa1

	lda #<mature_end ;ctll
	sta $930f
	lda #>mature_end
	sta $9310

	jsr $9312 ; exomizer: decrunch_file

	inc $01
	cli

	lda $d016
	eor #16
	sta $d016

	lda #0
	sta $dd00
	lda #48
	sta $d011
	lda #8
	sta $d018


	lda #250
	jsr timer

;	jmp *

;

.include spriteshow.asm

	lda #$f0
	cmp $d012
	bne *-3

	ldx #0
	lda #32	;10
kurrwa3	sta $6800,x
	sta $6900,x
	sta $6a00,x
	sta $6b00-10,x
	dex
	bne kurrwa3

	ldx #4
	stx temp

kupaaa	ldx temp
	lda blfadein,x
	sta ruchaczkolor

	ldx #10
	jsr timer

	ldx temp
	dex
	stx temp

	bpl kupaaa

loadloading
        lda #fnamel2
	ldx #<fname2
	ldy #>fname2

	jsr loadfile
        bcs *-3

spacjaa2

;	jsr $ffe4
;	cmp #32

	lda $dc01
	cmp #239
	bne spacjaa2
;;;

	ldx #4
	stx temp

kupaaaa	ldx temp
	lda blfadeout,x
	sta ruchaczkolor

	ldx #10
	jsr timer

	ldx temp
	dex
	stx temp

	bpl kupaaaa

; disable irqs after spriteshow
	sei
	lda #<irq
	ldy #>irq
	sta $0314
	sty $0315

	lda #$30
	sta $d012
	lda $d011
	and #127
	sta $d011
	cli

	lda $d011
	eor #32
	sta $d011


; reenable mcol mode + no hscroll + 40cols
; - put it later in diff. place
	lda #16+8
;	eor #16
	sta $d016

	lda $d011
	and #255-8
	sta $d011	; en 25-lines mode
			; after spritez
;
	lda #0
	sta $d015

	lda $d011
	eor #32
	sta $d011

	jsr show2000	; 69positions logo

	ldx #100
	jsr timer

	ldx #100
	jsr timer

	jsr dapr2000

	jsr loadloda

loadmain
        lda #fnamel1
	ldx #<fname1
	ldy #>fname1

	jsr loadfile
	bcs *-3

       ; lda $d011
       ; ora #16
       ; sta $d011

	jsr kupaz

	jsr zakryjloda

        lda #0
        sta $d020
	jmp $8000
;

loaderror
;	lda #<loaderrort
;	ldy #>loaderrort
;	jsr $ab1e
xxx     jmp $fce2
;jmp xxx

;

timer ; wait some time
        ;ldx #5

if skiptimer = 1
	rts
eif

__af     lda $d012
        cmp #$f1
        bne __af
        lda #$80
zuox     nop
        nop
        nop
        nop
        nop
        nop
        lsr
        bne zuox
;        dec $d020
        dex
        bne __af

	rts


irq	inc $d019

;	lda #178
;	sta $d012
;	lda $d011
;	and #127
;	sta $d011
;	jmp dalej_

hiirq	lda #$30
	sta $d012
	lda $d011
	and #127
	sta $d011

	jsr $1003

dalej_  jmp irqex

fname1 .text "1001100100000000" ; main
fnamel1 = *-fname1

fname2 .text "1000000000000000" ; lodaing pic
fnamel2 = *-fname2

;
reset_sid
         ldy #27
         lda #0
_rs      sta $d400,y
         dey
         bpl _rs
         rts
;;


;;;

*=$0fe0
.scru "ramos , odejdz od tego sida!!!!!"

.incprg intromuz.prg,l ; $1000-$1fff


;; there is ~250 bytes free after music so let's use them for
; some additional routines

zakryj_start

ldx #0
_zakryja
	lda #32
	sta 1024,x
	sta 1024+40,x
	sta 1024+80,x
	sta 1024+120,x
	sta 1024+160,x
	sta 1024+200,x
	sta 1024+240,x
	sta 1024+280,x
	sta 1024+320,x
	sta 1024+360,x
	sta 1024+400,x
	sta 1024+440,x
	sta 1024+480,x
	sta 1024+520,x
	sta 1024+560,x
	sta 1024+600,x
	sta 1024+640,x
	sta 1024+680,x
	sta 1024+720,x
	sta 1024+760,x
	sta 1024+800,x
	sta 1024+840,x
	sta 1024+880,x
	sta 1024+920,x
	sta 1024+960,x

_tia	;inc $d021
	;dec $d021
	lda $d012
	cmp #5

	bne _tia

_tia2	lda $d012
	cmp #7

	bne _tia2

	inx
	cpx #40
	bne _zakryja

	rts
;;;;;;;;;;;;;;;;;;;;

zakryj_start2

	ldx #40
_zakryj
_ti	;inc $d021
	;dec $d021
	lda $d012
	cmp #5
	bne _ti

	lda #160
	dex

	sta 1024,x
	sta 1024+40,x
	sta 1024+80,x
	sta 1024+120,x
	sta 1024+160,x
	sta 1024+200,x
	sta 1024+240,x
	sta 1024+280,x
	sta 1024+320,x
	sta 1024+360,x
	sta 1024+400,x
	sta 1024+440,x
	sta 1024+480,x
	sta 1024+520,x
	sta 1024+560,x
	sta 1024+600,x
	sta 1024+640,x
	sta 1024+680,x
	sta 1024+720,x
	sta 1024+760,x
	sta 1024+800,x
	sta 1024+840,x
	sta 1024+880,x
	sta 1024+920,x
	sta 1024+960,x

	bne _zakryj

	rts

; koniec przerwania
irqex
	pla
	tay
	pla
	tax
	pla
	rti


;;;;;;;;;;;;;;;;;;;;;;;;;


.incprg introg.prg,l ;$2000-$4800


	*= $4800

.incbin "object_0.isp"
.incbin "object_1.isp"
.incbin "object_2.isp"
.incbin "object_3.isp"
.incbin "object_4.isp"
.incbin "object_5.isp"
; -> $6600

.incbin "chareat.raw"	; char eater ;-) #152 in bank
.byte 0

;
; $7000- $71ff
;.incprg fontbzyk.prg,$7000  ; [temporary] font for scroll
.incprg fontodyn.prg,$7000  ; [temporary] font for scroll

mature = $7800
.incbin m.prg ; packed MATURE ERSB image , to depack @$e000
mature_end = mature + 873 ; 7b47


.incprg loader.prg,l ;$8000-8fff

.include deexo.asm ; $9300  - depacker

;.incprg names.prg,l ; 9500

;fade colors - from black to white

ruchaczkolor
.byte 0

temp
.byte 0

blfadein
.byte 1,15,12,11,0

blfadeout
.byte 0,11,12,15,1

.include names.asm ; $9500  - names

moveup

X	set $6800+17*40;(1024+40+16*40)
;	REPEAT  959
	REPEAT  8*40-1	; 9 linii

	X SET  X + 1
;echo X

	 lda X
	 sta X - 40
	 ;  DC      X,Y

	    REPEND

	rts

;	*= $a000

stext
.include "scroltext.asm"
upstext
.include "upstext.asm"

dosc
.byte 00

.include lodairq.asm

.include loadloda.asm

;;;;;

kupaz	ldx #20
	jsr timer
	rts

;;;;;

; IRQ ENTRY (when kernal rom is switched off and this shitty piece of code is needed)
; adapted from kernal rom

irqentry
 PHA
 TXA
 PHA
 TYA
 PHA
 JMP ($0314)   ; Vector: BRK Instruction Interrupt Address

;;

; table with $0400 textscreen linestarts
a0400tabl = *
0400table
XX	set $0400;(1024+40+16*40)
	REPEAT  25
; > = HI
 .byte >XX	; hi
 .byte <XX	; lo
XX SET  XX + 40
	    REPEND

a3f40tabl = *
3f40table
XX	set $3f40;(1024+40+16*40)
	REPEAT  25	; 9 linii
 .byte >XX	; hi
 .byte <XX	; lo
	XX SET  XX + 40
	    REPEND

ad800tabl = *
d800table
XX	set $d800;(1024+40+16*40)
	REPEAT  25	; 9 linii
 .byte >XX	; hi
 .byte <XX	; lo
	XX SET  XX + 40
	    REPEND


a4328tabl = *
4328table
XX	set $4328;(1024+40+16*40)
	REPEAT  25	; 9 linii
 .byte >XX	; hi
 .byte <XX	; lo
	XX SET  XX + 40
	    REPEND

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; 69 positoins logo

.include show2000.asm

;;;
; LODAING sprites

.include lodaspr.asm


.incprg logo2f.prg,$c000 ;$c000-c800 ; logo 2 fontz (Bank $c000-ffff)

; texts - put here to lower pre $1000 mem usage

text1 .text "                                        "
      .text "vulture design bios initialized    [ok]  "
      .text "                                        "
.byte 13,0

loado .text "dreamload initialized              [ok]"
.byte 13,0

text2 .text "couln't initialize fastloader. halted. "
      .text "kernel panic. see docs for solution.    "
.byte 13,0

t1541 .text "found a 1541 drive on iec bus"
.byte 13,0

t1570 .text "found a 1570 drive on iec bus"
.byte 13,0

t1571 .text "found a 1571 drive on iec bus"
.byte 13,0

t1581 .text "found a 1581 drive on iec bus"
.byte 13,0

t6581
.byte 18
      .text "6581 sid detected - use 8580     [fail]"
.byte 146,13,0

t8580 .text "8580 sid chip present              [ok]"
.byte 13,0

inite .text "                                        "
      .text "system initialized properly.            "
      .text "wait few seconds or press space to go."
.byte 13,0

c128d .text "commodore 128 detected"
.byte 13,0

c64de .text "commodore 64 detected"
.byte 13,0

	*=$cb00	; sprite #44

.incbin "kulka.raw"

;.incprg logoz.prg,l ;$a000-bfff - old logo2 i n3rd bank- split into 'F' and 'G' and put into 4th bank
;
.incprg logo2g.prg,l ;$e000-ffff ; logos 2 gfx



